extends Node2D


# Called when the node enters the scene tree for the first time.
func _ready():
	GlobalSignal.battleStart.connect(onBattleStart)
	
	
func newGame():
	var playerData = CustomClasses.EntityData.new()
	var newGameSaveDataConfig = ConfigClasses.NewGameSaveDataConfig.configs[0]
	playerData.newBorn(ConfigClasses.CareerConfig.configsDict[newGameSaveDataConfig.playerCareer])
	GlobalData.playerData = playerData
	playerData.spriteFrames = newGameSaveDataConfig.spriteFrames
	playerData.uiHeadIcon = newGameSaveDataConfig.uiHeadIcon
	
	var weaponData = CustomClasses.WeaponData.new()
	weaponData.config = ConfigClasses.WeaponConfig.configsDict[newGameSaveDataConfig.weapon]
	weaponData.lv = 1
	playerData.weaponsData.append(weaponData)
	GlobalData.addWeapon(weaponData)
	
	
	var headEquipmentData = CustomClasses.EquipmentData.new()
	headEquipmentData.config = ConfigClasses.EquipmentConfig.configsDict[newGameSaveDataConfig.headEquipment] 
	playerData.equipmentsDataDict[Consts.EQUIPMENT_LOC_HEAD] = headEquipmentData
	playerData.equipmentsData.append(headEquipmentData)
	GlobalData.addEquipment(headEquipmentData)
	
	var bodyEquipmentData = CustomClasses.EquipmentData.new()
	bodyEquipmentData.config = ConfigClasses.EquipmentConfig.configsDict[newGameSaveDataConfig.bodyEquipment] 
	playerData.equipmentsDataDict[Consts.EQUIPMENT_LOC_BODY] = bodyEquipmentData
	playerData.equipmentsData.append(bodyEquipmentData)
	GlobalData.addEquipment(bodyEquipmentData)
	
	var legEquipmentData = CustomClasses.EquipmentData.new()
	legEquipmentData.config = ConfigClasses.EquipmentConfig.configsDict[newGameSaveDataConfig.legEquipment] 
	playerData.equipmentsDataDict[Consts.EQUIPMENT_LOC_LEG] = legEquipmentData
	playerData.equipmentsData.append(legEquipmentData)
	GlobalData.addEquipment(legEquipmentData)
	
	var handEquipmentData = CustomClasses.EquipmentData.new()
	handEquipmentData.config = ConfigClasses.EquipmentConfig.configsDict[newGameSaveDataConfig.handEquipment] 
	playerData.equipmentsDataDict[Consts.EQUIPMENT_LOC_HAND] = handEquipmentData
	playerData.equipmentsData.append(handEquipmentData)
	GlobalData.addEquipment(handEquipmentData)
	
	Global.transiteScene(newGameSaveDataConfig.sceneTransitionId)
	

func onBattleStart(battleTransitionConfigId):
	var config = ConfigClasses.BattleTransitionConfig.configsDict[battleTransitionConfigId]
	var fieldSizeX = int(config.fieldSizeX)
	var fieldSizeY = int(config.fieldSizeY)
	
	BattleData.setBattleFieldSize(fieldSizeX, fieldSizeY)
	
	var battleEnemyGroupArr = ConfigClasses.BattleEnemyGroupConfig.groupConfigArrDict[config.enemyGroup]
	for battleEnemyConfig in battleEnemyGroupArr:
		var enemyConfig = ConfigClasses.EnemyConfig.configsDict[battleEnemyConfig.enemy]
		var enemyPos = Global.str2Vec(battleEnemyConfig.pos)
		var enemyFace = battleEnemyConfig.face
		
		var enemyData = CustomClasses.EntityData.new()
		enemyData.newBorn(ConfigClasses.CareerConfig.configsDict[enemyConfig.career])
		enemyData.battleFace = enemyFace
		enemyData.battlePos = enemyPos
		enemyData.battleIsEnemy = true
		enemyData.spriteFrames = enemyConfig.spriteFrames
		BattleData.addEnemy(enemyData)
	
	var heroPosX = 0
	var heroPosY = 2
	var heroFace = "Down"
	GlobalData.playerData.battlePos = Vector2(heroPosX, heroPosY)
	GlobalData.playerData.battleFace = heroFace
	GlobalData.playerData.battleIsEnemy = false
	BattleData.addHero(GlobalData.playerData)
	
	for partner in GlobalData.partnersData:
		heroPosX += 1
		
		partner.battlePos = Vector2(heroPosX, heroPosY)
		partner.battleFace = heroFace
		partner.battleIsEnemy = false
		BattleData.addHero(partner)

	GlobalData.enterBattleBgImage = Global.captureCurrentViewTexture()
	Global.changeScene("BattleField")
	

func _physics_process(delta):
	if Console.visible:
		return
		
	if Input.is_action_just_pressed("Inventory"):
		if not Inventory.visible:
			Inventory.showAll()
		else:
			Inventory.hideAll()


